package com.qeado.admin.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.qeado.admin.common.BaseConstData;
import com.qeado.admin.model.Menu;
import com.qeado.admin.model.Role;
import com.qeado.admin.service.RoleService;
import com.qeado.common.helper.AttributeHelper;

@Controller
public class RoleController {
	@Autowired
	private RoleService roleService;
	
	@RequestMapping("/roleManage.ac")
	public String roleManage(HttpServletRequest request){
		AttributeHelper.removeSessionAttribute(request, "T_");
		return "admin/role_search";
	}
	
	@RequestMapping("/newRole.ac")
	public String newRole(){
		return "admin/role_manage";
	}
	
	@RequestMapping("/roleSearch.ac")
	public ModelAndView searchRole(HttpServletRequest request, Role roleInfo){
		List<Role> roleList = roleService.searchRole(roleInfo);
		request.setAttribute("role", roleInfo);
		return new ModelAndView("admin/role_search","roleList",roleList);
	}
	
	@RequestMapping("/saveRole.ac")
	public ModelAndView saveRole(HttpServletRequest request, Role roleInfo){
		roleInfo.setCreator(request.getSession().getAttribute(BaseConstData.USER_ID).toString());
		String[] menuIds = request.getParameterValues("checkList");
		String message = roleService.saveRole(roleInfo, menuIds);
		if(message != null) {
			return new ModelAndView("admin/role_manage","message",message);
		} else {
			return new ModelAndView("admin/role_manage","roleMenuList",roleService.searchRoleMenuList(roleInfo.getId()));			
		}
		
	}
	
	@RequestMapping("/selectRole.ac")
	public String selectRole(HttpServletRequest request, String roleId) {
		Role roleInfo = roleService.selectRole(roleId);
		List<Menu> roleMenuList = roleService.searchRoleMenuList(roleId);
		request.setAttribute("role", roleInfo);
		request.setAttribute("roleMenuList", roleMenuList);
		return "admin/role_manage";
	}
	
	@RequestMapping("/deleteRole.ac")
	public ModelAndView deleteRole(String roleId){
		roleService.deleteRole(roleId);
		List<Role> roleList = roleService.searchRole(null);
		return new ModelAndView("admin/role_search","roleList",roleList);
	}

}
